--- permalink: python/heatmap-webgl/ description: How to make webGL based heatmaps in Python with Plotly. name: WebGL based Heatmaps | plotly has_thumbnail: true thumbnail: thumbnail/heatmap-webgl.jpg layout: user-guide name: WebGL Heatmaps language: python title: Python Heatmaps WebGL | plotly display_as: scientific has_thumbnail: true page_type: example_index order: 4 --- {% raw %}

WebGL based Heatmaps with Plotly

We start by importing the relevant modules:

In [1]:
import plotly.offline as py
py.init_notebook_mode()

import PIL
import urllib, cStringIO
import numpy as np

Downloading the image:

In [2]:
image_url = 'https://images.plot.ly/plotly-documentation/images/heatmap-galaxy.jpg'
f = cStringIO.StringIO(urllib.urlopen(image_url).read())
img = PIL.Image.open(f)
In [3]:
img
Out[3]:

Processing the image for generating heatmap:

In [4]:
arr = np.array(img)
z_data = []
In [5]:
for i in range(500):
    k = []
    for j in range(500):
        k.append(sum(arr[i][j]))
    z_data.append(k)

Generating the heatmap:

In [6]:
trace = dict(type='heatmapgl', z=z_data)
In [7]:
py.iplot([trace], validate=False)
{% endraw %}